// preload images
var coin_0 = new Image();
coin_0.src = "coin-spin.gif";
var coin_h = new Image();
coin_h.src = "heads.png";
var coin_s = new Image();
coin_s.src = "side.png";
var coin_t = new Image();
coin_t.src = "tails.png";
//
var led_g0 = new Image();
led_g0.src = "green-off.png";
var led_g1 = new Image();
led_g1.src = "green-on.png";
var led_y0 = new Image();
led_y0.src = "yellow-off.png";
var led_y1 = new Image();
led_y1.src = "yellow-on.png";
//end preload
// set variables or ram
var switch_state = 0;
var flip_flop;
var coin_state = 0;
var coin_fluke = 0;
var flash_on = 0;
var flash_timer;
//  real random for show number
function coin_roll() {
 coin_state = Math.ceil(Math.random() * 2);
 coin_fluke = Math.ceil(Math.random() * 100);
 coin_song = Math.ceil(Math.random() * 12);
if   (coin_fluke == 77){
    document.images.coin_image.src=coin_s.src;
    }
    else
    {
    heads_tails();
    }
flash_on = 1;
flash_flip();
 }
//here is the toss
function heads_tails() {
  if  (coin_state == 1 ){
   document.images.coin_image.src=coin_h.src;
    }
    else
    {
   document.images.coin_image.src=coin_t.src;
    }
 }
//
// user clicks the coin
function  spin_em(){
if (switch_state == 0 )
{
flash_on = 0;
clearTimeout(flip_flop);
switch_state=1;
document.images.led_yel.src=led_y1.src;
document.images.led_grn.src=led_g0.src;
document.images.coin_image.src=coin_0.src;
}
else
{
coin_off();
coin_roll();
}
}
//
// turn of  the process go to standby
//
function coin_off() {
switch_state = 0;
flash_on = 0;
clearTimeout(flip_flop);
document.images.led_yel.src=led_y0.src;
document.images.led_grn.src=led_g1.src;
}
// A led flasher indicates that you got the results
//
function flash_flip() {
if(flash_on == 1) {
document.images.led_yel.src=led_y0.src;
document.images.led_grn.src=led_g1.src;
flip_flop = window.setTimeout('flash_flop();',100);
}
}
//
function flash_flop() {
document.images.led_yel.src=led_y1.src;
document.images.led_grn.src=led_g0.src;
flip_flop = window.setTimeout('flash_flip();',100);
}
//
window.onload=spin_em;
// end of the story
